#!/usr/bin/python3
# -*- coding:utf-8 -*-
# 获取天天基金的FOF持仓

import requests
import csv
import json
import re
import datetime
import sys
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from bs4 import BeautifulSoup
import lxml


url = 'http://fund.eastmoney.com/%s.html'

fofurl = 'http://fund.eastmoney.com/Data/Fund_JJJZ_Data.aspx?t=1&lx=15&letter=&gsid=&text=&sort=bzdm,asc&page=1,9999&dt=1627296220627&atfc=&onlySale=0'


# 获取FOF基金列表
def get_fof_ids(url):
    html = requests.get(url)
    if html.status_code == 200:
        text = html.text
        index = text.find('[[')
        rindex = text.rfind(']]')
        text = text[index:rindex+2]
        foflist = eval(text)
        fofidlist = [id[0] for id in foflist]
        return fofidlist


# 获取FOF基金的基金持仓
def get_funds_of_fof(url):
    html = requests.get(url)
    html.encoding='utf-8'
    if html.status_code == 200:
        text = html.text
        text = text.replace('><', '>\n<')
        # funds = re.findall('<a href="http://fund.eastmoney.com/(.*?).html" title=\'.*?\'>(.*?)</a>  </td>  <td class="alignRight bold">(.*?)%</td>',text)
        funds = re.findall('<a href="http://fund.eastmoney.com/(.*?).html" title=\'.*?\'>(.*?)</a>  </td>  <td class="alignRight bold">.*?%</td>',text)
        return funds


# 获取基金的股票持仓
def get_stocks_of_fund(url):
    html = requests.get(url)
    html.encoding='utf-8'
    if html.status_code == 200:
        text = html.text
        text = text.replace('><', '>\n<')
        stocks = re.findall('<a href="http://quote.eastmoney.com/(.*?).html" title=".*?">(.*?)</a>  </td>  <td class="alignRight bold">(.*?)</td>',text)
        return stocks

# 获取基金的债券持仓
def get_bonds_of_fund(url):
    html = requests.get(url)
    html.encoding='utf-8'
    if html.status_code == 200:
        text = html.text
        soup = BeautifulSoup(html.text,'lxml')
        trs = soup.find('li', 'position_bonds').find('table').find_all('tr')
        bonds = []
        for tr in trs:
            tds = tr.find_all('td')
            bond = []
            for td in tds:
                if td.string == None:
                    bond.append(td['newcode'])
                else:
                    bond.append(td.string)

            if len(bond) > 0:
                bonds.append(bond)
        return bonds

if __name__ == '__main__':
    fof_ids = get_fof_ids(fofurl)
    fof_funds = {}
    fund_fofs = {}
    fund_ids = {}
    for fof_id in fof_ids:
        print("开始获取%sFOF的基金持仓!" % fof_id)
        funds = get_funds_of_fof(url % fof_id)
        # stocks = get_stocks_of_fund(url % fof_id)
        for fund in funds:
            if fund in fund_fofs:
                fund_fofs[fund].append(fof_id);
            else:
                fund_fofs[fund] = [fof_id];
        # print(funds)
        # print(stocks)
        if len(funds) > 0:
            fof_funds[fof_id] = funds
    fund_fof_count = {}
    for fund_fof in fund_fofs:
        fund_fof_count[fund_fof] = len(fund_fofs[fund_fof])
    # print (fund_fof_count)
    df = pd.DataFrame.from_dict(fund_fof_count, orient='index',columns=['Count'])
    df.sort_values(by='Count',inplace=True)
    # bonds = get_bonds_of_fund(url % '001250')
    # # print(bonds)
    # df = pd.DataFrame(bonds)
    print(df.tail(20))
